//
//  JWCustomButton.m
//  JWMovie
//
//  Created by Jorn.Wu on 15/7/22.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "JWCustomButton.h"
#import "Star.h"
#import "UIImage+MultiFormat.h"
#import "JWMovieCellModel.h"
#import "JWStarView.h"

@implementation JWCustomButton

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.mImageVeiw = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self addSubview:self.mImageVeiw];
        
        self.mTitle = [[UILabel alloc] initWithFrame:CGRectZero];
        self.mTitle.numberOfLines = 0;
        self.mTitle.font = [UIFont systemFontOfSize:15];
        [self addSubview:self.mTitle];
        
        self.mOriginal_title = [[UILabel alloc] initWithFrame:CGRectZero];
        self.mOriginal_title.numberOfLines = 0;
        self.mOriginal_title.font = [UIFont systemFontOfSize:15];
        [self addSubview:self.mOriginal_title];
        
        self.mYear = [[UILabel alloc] initWithFrame:CGRectZero];
        self.mYear.font = [UIFont systemFontOfSize:15];
        [self addSubview:self.mYear];
        
        self.mRate = [[UILabel alloc] initWithFrame:CGRectZero];
        self.mRate.textColor = [UIColor whiteColor];
        [self addSubview:self.mRate];
        
        self.mStarView = [[JWStarView alloc] initWithFrame:CGRectZero];
        [self addSubview:self.mStarView];
        
        

     
        
    }
    return self;
}

- (void)setMovieModel:(JWMovieCellModel *)movieModel{
    _movieModel = movieModel;
    
    NSString *urlStr = [_movieModel.images objectForKey:@"large"];
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]];
    UIImage *image = [UIImage sd_imageWithData:imageData];
    
    self.mImageVeiw.image = image;
    
    self.mTitle.text = [NSString stringWithFormat:@"中文名：%@",_movieModel.title];
    
    self.mOriginal_title.text = [NSString stringWithFormat:@"原名：%@",_movieModel.original_title];
    
    NSString *str = [NSString stringWithFormat:@"年份：%.f",[_movieModel.year floatValue] ];
    self.mYear.text = str;
    
    NSDictionary *rating = _movieModel.rating;
    float rate  = [[rating objectForKey:@"average"] floatValue];
    self.mRate.text = [NSString stringWithFormat:@"%.1f",rate];
    
    self.mStarView.rate = rate;
    
    
    
    [self setNeedsLayout];
 
    
}
- (void)layoutSubviews{
    [super layoutSubviews];
    self.mImageVeiw.frame = CGRectMake(10, 100, 100, 150);
    self.mTitle.frame = CGRectMake(120, 100, 110, 60);
    self.mOriginal_title.frame = CGRectMake(120, 160, 110, 60);
    self.mYear.frame = CGRectMake(120, 220, 110, 20);
    self.mStarView.frame = CGRectMake(10, 250, 100, 50);
    self.mRate.frame = CGRectMake(200, 250, 40, 30);
    
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
